package com.ning.google;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class SendMailManager2 {

	
	private static SendMailManager2 _instance = new SendMailManager2();

	public static SendMailManager2 instance() {
		return _instance;
	}
	
	public String send(){
		Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
        Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, -1);
		String date =  new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		
        String msgBody = "<div style=\"width:620px; border:1px solid #ccc;padding:5px;\">"+
      "<table style=\"width:600px; border:0; margin:0 auto;border-collapse:collapse;border-spacing:0; \">"+
		"<tr>"+
		   "<td>"+
			"<div style=\"min-height:45px;font-size:20px;font-weight:bold;max-height:45px; text-align:center;border-bottom:1px solid #ccc;\">统计日报【"+date+"】</div>"+
		"</td>"+
		"</tr>"+
		"<tr>"+
		   "<td>"+
			"<div style=\"font-size:14px; padding:10px;color:#2C2C2C;line-height:20px; text-align:left;border-bottom:1px dashed #ccc;\">"+
				"<p style=\"color:#0088CC;padding:0px;margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;\"><strong>概况信息</strong></p>"+
			"</div>"+
		"</td>"+
		"</tr>"+
		"<tr>"+
		   "<td>"+
			 "<table style=\"width:600px; border:0;font-size:12px; margin:0 auto;border-collapse:collapse;border-spacing:0;\">"+
				"<tr>"+
					"<td width=\"100\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">日期</td>"+
					"<td width=\"100\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">PV</td>"+
					"<td width=\"100\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">UV</td>"+
					"<td width=\"100\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">新UV</td>"+
					"<td width=\"100\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">新UV占比</td>"+
					"<td width=\"100\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">平均停留时间(s)</td>"+
				"</tr>";
        List<GeneralInfo> list1 = GAnalyticsAutoManager2.instance().getGeneralInfo();
        if(list1 != null){
        	for(GeneralInfo info1 : list1){
        		msgBody += "<tr>"+
					"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+info1.getDate()+"</td>"+
					"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+info1.getPv()+"</td>"+
					"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+info1.getUv()+"</td>"+
					"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+info1.getNewuv()+"</td>"+
					"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+info1.getPercentNewUv()+"%</td>"+
					"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+info1.getAvgtime()+"</td>"+
				"</tr>";
        	}
        }
        msgBody += "</table>"+
		"</td>"+
	"</tr>"+
	"<tr>"+
	   "<td>"+
		"<div style=\"font-size:14px; padding:10px;color:#2C2C2C;line-height:20px; text-align:left;border-bottom:1px dashed #ccc;\">"+
			"<p style=\"color:#0088CC;padding:0px;margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;\"><strong>App概况信息</strong></p>"+
		"</div>"+
	"</td>"+
	"</tr>"+
	"<tr>"+
	   "<td>"+
	   	"<table style=\"width:600px; font-size:12px;border:0; margin:0 auto;border-collapse:collapse;border-spacing:0; \">";
        HashMap<String,GeneralInfo> appdata = GAnalyticsAutoManager2.instance().getAppDetailsInfo();
        Iterator<String> appdatas = appdata.keySet().iterator();
        msgBody += "<tr><td  style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">项目</td>" +
        		"<td width=\"70\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">PV</td>" +
        		"<td width=\"70\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">UV</td>" +
        		"<td width=\"90\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">每次访问页数</td>" +
        		"<td width=\"70\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">停留时间</td>" +
        		"<td width=\"70\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">跳出率</td>" +
        		"<td width=\"70\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">新用户</td>" +
        		"<td width=\"70\" style=\"font-weight:bold;text-align:center;padding:5px 0;border:1px dotted #CBCBCB;background:#eee;\">新访问占比</td>" +
        		"</tr>";
        GeneralInfo allapp = appdata.get("All");
        GeneralInfo picapp = appdata.get("picapp");
        GeneralInfo newsapp = appdata.get("newsapp");
        GeneralInfo toolsapp = appdata.get("toolsapp");
        int totalPv = allapp.getPv();
    	int totalUv = allapp.getUv();
    	NumberFormat format = new DecimalFormat("#0.00");
        msgBody += "<tr>" +
        		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">所有</td>" +
        		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+totalPv+"</td>" +
        		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+totalUv+"</td>" +
        		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+allapp.getPerPageUser()+"</td>" +
        		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+allapp.getAvgtime()+"</td>" +
        		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+allapp.getExitRate()+"%</td>" +
        		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+allapp.getNewuv()+"</td>" +
        		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+allapp.getPercentNewUv()+"%</td>" +
        		"</tr>";
        msgBody += "<tr>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">相册App</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+picapp.getPv()+"("+format.format((double)picapp.getPv()*100/(double)totalPv)+"%)</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+picapp.getUv()+"("+format.format((double)picapp.getUv()*100/(double)totalUv)+"%)</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+picapp.getPerPageUser()+"</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+picapp.getAvgtime()+"</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+picapp.getExitRate()+"%</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+picapp.getNewuv()+"</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+picapp.getPercentNewUv()+"%</td>" +
		"</tr>";
        
        msgBody += "<tr>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">资讯App</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+newsapp.getPv()+"("+format.format((double)newsapp.getPv()*100/(double)totalPv)+"%)</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+newsapp.getUv()+"("+format.format((double)newsapp.getUv()*100/(double)totalUv)+"%)</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+newsapp.getPerPageUser()+"</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+newsapp.getAvgtime()+"</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+newsapp.getExitRate()+"%</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+newsapp.getNewuv()+"</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+newsapp.getPercentNewUv()+"%</td>" +
		"</tr>";
        
        msgBody += "<tr>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">装备App</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+toolsapp.getPv()+"("+format.format((double)toolsapp.getPv()*100/(double)totalPv)+"%)</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+toolsapp.getUv()+"("+format.format((double)toolsapp.getUv()*100/(double)totalUv)+"%)</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+toolsapp.getPerPageUser()+"</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+toolsapp.getAvgtime()+"</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+toolsapp.getExitRate()+"%</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+toolsapp.getNewuv()+"</td>" +
		"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+toolsapp.getPercentNewUv()+"%</td>" +
		"</tr>";
        
        List<GeneralInfo> appsinfo = new ArrayList<GeneralInfo>();
        while(appdatas.hasNext()){
        	String key = appdatas.next();
        	if(key.equals("All") || key.equals("picapp") || key.equals("newsapp") || key.equals("toolsapp")) continue;
        	GeneralInfo tempdata = appdata.get(key);
        	tempdata.setType(key);
        	appsinfo.add(tempdata);
        }
        ComparatorPV comparator=new ComparatorPV();
        Collections.sort(appsinfo, comparator);
        
       for(GeneralInfo g : appsinfo){
        	msgBody += "<tr>" +
        			"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+g.getType()+"</td>" +
        			"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+g.getPv()+"("+format.format((double)g.getPv()*100/(double)totalPv)+"%)</td>" +
        			"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+g.getUv()+"("+format.format((double)g.getUv()*100/(double)totalUv)+"%)</td>" +
        			"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+g.getPerPageUser()+"</td>" +
        			"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+g.getAvgtime()+"</td>" +
        			"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+g.getExitRate()+"%</td>" +
        			"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+g.getNewuv()+"</td>" +
        			"<td style=\"text-align:center;padding:5px 0;border:1px dotted #CBCBCB;\">"+g.getPercentNewUv()+"%</td></tr>";
        }
        msgBody += "</table>"+
       "</td>"+
    "</tr>"+
		
	 "</table>"+
	 "</div>";
        try {
    		
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("zhufukc@gmail.com"));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress("zhufukc@gmail.com"));
            msg.setSubject(MimeUtility.encodeText("统计日报 【"+date+"】","gb2312","B"));  
            Multipart mp = new MimeMultipart();

            MimeBodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent(msgBody, "text/html");
            mp.addBodyPart(htmlPart);

            msg.setContent(mp);
            
            Transport.send(msg);
           
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return msgBody;
	}
	
	public class ComparatorPV implements Comparator<GeneralInfo>{
		 public int compare(GeneralInfo g1, GeneralInfo g2) {
			 return g2.getPv() - g1.getPv();
		 }	 
	}
}
